package com.fbinggun.playeraddattribute;

import com.alibaba.fastjson.JSONObject;
import com.fbinggun.fattribute.attribute.IAttribute;
import org.bukkit.entity.Player;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

public class PlayerData {
    String player;
    Map<IAttribute,Double> map = new HashMap<>();

    public PlayerData(String player) {
        this.player = player;
    }

    public PlayerData(String player, Map<IAttribute, Double> map) {
        this.player = player;
        this.map = map;
    }
    public String toJSONObject(){
        JSONObject jsonObject = new JSONObject();
        for(IAttribute iAttribute:map.keySet()) {
            jsonObject.put(iAttribute.getKey(),map.get(iAttribute));
        }
        return jsonObject.toString();
    }

    public void add(String key,double value){
        IAttribute iAttribute = IAttribute.getKey(key);
        if(iAttribute==null){
        return;
        }
        if (map.get(iAttribute) != null) {
            map.put(iAttribute,value+map.get(iAttribute));
        }else {
            map.put(iAttribute,value);
        }
    }
    public double getAttribute(String key){
        return map.getOrDefault(IAttribute.getKey(key),0d);
    }
    public void set(String key,double value){
        IAttribute iAttribute = IAttribute.getKey(key);
        if(iAttribute==null){
            return;
        }
        map.put(iAttribute,value);
    }

    public void save() throws SQLException, ClassNotFoundException {
        PlayerAddAttribute.tabel.setJson(this);
    }



}
